package com.wws.family.adapter

import android.app.Activity
import android.app.ActivityOptions
import android.content.Intent
import android.view.LayoutInflater
import android.view.ViewGroup
import com.wika.basics.base.BaseBindViewHolder
import com.wika.basics.base.BaseRecyclerViewAdapter
import com.wika.basics.ext.toColorSpan
import com.wika.basics.ext.toSizeSpan
import com.wws.family.R
import com.wws.family.databinding.ItemMedicineBinding
import com.wws.family.db.MedicineBean
import com.wws.family.ui.activity.MedicineDetailActivity
import com.wws.family.utils.DateUtils

/**
 *@Description:药品
 *@Date: 2025/1/7 22:28
 *@Author: WangWeiShuo
 */
class MedicineAdapter(val context: Activity):BaseRecyclerViewAdapter<MedicineBean,ItemMedicineBinding>() {

    private var deleteCallBack: OnDeleteCallBack? = null

    override fun onBindDefViewHolder(
        holder: BaseBindViewHolder<ItemMedicineBinding>,
        item: MedicineBean?,
        position: Int
    ) {
        holder.binding.tvMedicineName.text = item?.name
        holder.binding.tvMedicineFunction.text = item?.function
        when(item?.type){
            0 -> {
                holder.binding.ivMedicineIcon.background = context.getDrawable(R.drawable.icon_yaowan)
            }
            1 -> {
                holder.binding.ivMedicineIcon.background = context.getDrawable(R.drawable.icon_koufuye)
            }
            2 -> {
                holder.binding.ivMedicineIcon.background = context.getDrawable(R.drawable.icon_chongji)
            }
            else -> {
                holder.binding.ivMedicineIcon.background = context.getDrawable(R.drawable.icon_medicine_1)
            }
        }
        val time = item?.endTime
        val remainDay = DateUtils.getBetweenTime(DateUtils.getNowDate(),time)
        if(remainDay > 0) {
            val day = "剩余${remainDay}天"
            val daySpanSize = day.toSizeSpan(IntRange(2,day.length - 1),1.5f)
            val daySpanColor = daySpanSize.toColorSpan(IntRange(2,day.length - 1),context.getColor(
                com.wika.basics.R.color.blue_1E90FF))
            holder.binding.tvRecentStatus.text = daySpanColor
            holder.binding.tvRecentStatus.setTextColor(context.getColor(R.color.black))
        }else if(remainDay == 0L){
            holder.binding.tvRecentStatus.text = "今天到期"
            holder.binding.tvRecentStatus.setTextColor(context.getColor(R.color.green_00C853))
        }else{
            holder.binding.tvRecentStatus.text = "已过期"
            holder.binding.tvRecentStatus.setTextColor(context.getColor(R.color.red_DD2C00))
        }

        holder.binding.medicineLayout.setOnClickListener {
            val intent = Intent(context, MedicineDetailActivity::class.java)
            // 创建ActivityOptions
            val options = ActivityOptions.makeSceneTransitionAnimation(
                context, holder.binding.ivMedicineIcon, "sharedImage").toBundle()
            intent.putExtra("medicineItem",item)
            context.startActivity(intent, options)
        }

        holder.binding.itemDelete.setOnClickListener {
            if(deleteCallBack != null){
                deleteCallBack?.onDelete(position,item)
            }
        }
    }

    override fun getViewBinding(
        layoutInflater: LayoutInflater,
        parent: ViewGroup,
        viewType: Int
    ): ItemMedicineBinding {
        return ItemMedicineBinding.inflate(layoutInflater,parent,false)
    }

    fun addDeleteCallBack(callBack: OnDeleteCallBack){
        deleteCallBack = callBack
    }

    interface OnDeleteCallBack{
        fun onDelete(pos: Int,itemBean: MedicineBean?)
    }
}